<template>
  <div>
    <!-- 顶部 -->
    <van-nav-bar title="用户注册" />
    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button square block type="primary" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
    <!-- 下面跳转 -->
    <div class="text-right">
      <router-link to="/login">有账号？去登陆</router-link>
    </div>
  </div>
</template>

<script>
//导入封装好的注册方法
import { register } from "../api/user";
import { Notify } from "vant";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async onSubmit() {
      // 表单提交，调用注册接口
      const res = await register({
        // await会等接口调用完成之后再执行后面的代码
        username: this.username,
        password: this.password,
      });
      // 成功通知
      Notify({ type: "success", message: "注册成功" });
      //跳转到登录页面
      this.$router.push("/login");
    },
  },
};
</script>

<style scoped>
.text-right {
  text-align: right;
  margin-right: 16px;
}
</style>